package MyQueue;

public class MyCircularQueue {
    private int[] elem;
    private int first;
    private int end;
    private int size;
    private int maxNum;

    public MyCircularQueue(int k) {
        this.elem = new int[k];
        this.maxNum = k;
    }

    public boolean enQueue(int value) {
        if(this.isEmpty()) {
            this.elem[this.end++] = value;
            this.size++;
            return true;
        }else if(this.isFull()) {
            return false;
        }else {
            this.elem[this.end] = value;
            this.end = (this.end+1)%this.maxNum;
            this.size++;
            return true;
        }
    }

    public Integer deQueue() {
        if(this.isEmpty()) {
            return null;
        }else {
            int tmp = this.elem[this.first++];
            if(this.first >= this.maxNum) {
                this.first -= this.maxNum;
            }
            this.size--;
            return tmp;
        }
    }

    public int Front() {
        return this.elem[this.first];
    }

    public int Rear() {
        return this.elem[this.end];
    }

    public boolean isEmpty() {
        return this.size == 0;
    }

    public boolean isFull() {
        return this.size == this.maxNum;
    }
}
